import { userLogin, reqUserInfo ,reqLogOut} from "@/api"
const state = {
    token: localStorage.getItem('TOKEN'),
    userInfo:{}

}
const mutations = {

    USERINFO(state, token) {
        state.token = token
    },
    GETUSERINFO(state,userInfo){
        state.userInfo=userInfo
    },
    //退出登录
    LOGOUT(){
        state.token=''
        state.userInfo=''
        localStorage.removeItem("TOKEN")
    }
}
const actions = {
    async getUserLogin({ commit }, data) {
        let result = await userLogin(data)
        console.log(11111, result);
        if (result.code == 200) {
            commit('USERINFO', result.data.token)
            localStorage.setItem('TOKEN', result.data.token)
            return 'ok'
        } else {
            Promise.reject(new Error(faile))
        }

    },
    async getUserInfo({ commit }) {
        let result = await reqUserInfo()
        console.log(result);
        if (result.code == 200) {
            commit('GETUSERINFO',result.data)
        }
    },
    async getLogOut({commit}){
        let result = await reqLogOut()
        if(result.code==200){
            commit('LOGOUT')
        }
    }
}

const getters = {}
export default {
    state,
    mutations,
    actions,
    getters
}